package com.veetao.api.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jboss.netty.util.internal.StringUtil;

/**
 * 
 * @author guohongtao
 * @date 2017年8月29日
 */
public class StringUtils {
	public StringUtils() {
		super();
	}

	private static Pattern emailPattern = Pattern.compile("^([a-z0-9A-Z]+[-|_|\\.]*)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$");
//	private static Pattern emailPattern = Pattern.compile("^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$");
	public static boolean isEmailAddress(String email) {
		return emailPattern.matcher(email).matches();
	}
	private static final Pattern mobilePattern = Pattern.compile("^((13[0-9])|(14[0-9])|(15[0-9])|(16[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\\d{8}$");
	
	
	public static boolean isUrl(String url) {
		String regex = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(url);
		return matcher.matches();
	}
	
	public static boolean isNotMobile(String mobile) {
		return !isMobile(mobile);
	}
	
	public static boolean isMobile(String mobile) {
		Matcher m = mobilePattern.matcher(mobile);  
		return m.matches();  
	}
	private static Pattern namePattern = Pattern.compile("(.).*?(.)(@.+)?"); 
	public static String getMaskedUserName(String name) {
		Matcher matcher = namePattern.matcher(name);
		if(!matcher.matches()) {
			return name;
		}
		name = matcher.group(1) + "***" + matcher.group(2);
		
		if(matcher.groupCount() == 3 && matcher.group(3) != null) {
			name = name + matcher.group(3);
		}
		
		return name;
	}
	
	public static void main(String[] args) {
//		boolean s=isEmailAddress("hh_abc_assU@123.com");
//		String s = getMaskedUserName("hah@123.com");
//		String ss= "http://asdasd.cn";
//		boolean url = isUrl(ss);
//		System.out.println(s);

	}

}
